package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.bean.TlPaymentBean;
import com.jrx.anytxn.reporting.job.unionFile.step.*;
import com.jrx.anytxn.transaction.entity.TlPaymentLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成还款流水文件(按照创建时间 增量抽取 还款流水映射表)
 * 增加step 查询三方还款流水(临商普惠都是文件还款 记得三方还款表)
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateRepayFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateRepayFileConfiguration.class);


    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private UnionRepayFileListener unionRepayFileListener;
    @Autowired
    private UnionRepayFileProcessor unionRepayFileProcessor;
    @Autowired
    private UnionRepayFileWriter unionRepayFileWriter;
    @Autowired
    private UnionThirdRepayFileProcessor unionThirdRepayFileProcessor;
    @Autowired
    private UnionThirdRepayFileWriter unionThirdRepayFileWriter;

    @Bean
    public Job generateRepayFileJob(){
        return jobBuilderFactory.get("generateRepayFileJob").incrementer(new RunIdIncrementer()).listener(unionRepayFileListener)
                .start(generateRepayFileStep())
                .next(generateThirdRepayFileStep())
                .build();
    }

    @Bean
    public Step generateRepayFileStep(){
        return stepBuilderFactory.get("generateRepayFileStep")
                .<TlPaymentLog, TlPaymentBean>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(unionRepayFileReader())
                .processor(unionRepayFileProcessor)
                .writer(unionRepayFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlPaymentLog> unionRepayFileReader(){
        logger.info("生成联盟还款流水文件批量启动,机构：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        return new UnionRepayFileReader(sqlSessionFactory,anyTxnBatchProperties.getFileConfig().getPageSize(),org);
    }

    @Bean
    public Step generateThirdRepayFileStep(){
        return stepBuilderFactory.get("generateThirdRepayFileStep")
                .<TlThirdPartyPayment, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(thirdRepayFileReader())
                .processor(unionThirdRepayFileProcessor)
                .writer(unionThirdRepayFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlThirdPartyPayment> thirdRepayFileReader(){
        logger.info("生成三方还款流水文件批量启动,机构：{}", ReportingConstant.DEFAULT_ORG_NUMBER);
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);

        return new UnionThirdRepayFileReader(sqlSessionFactory,anyTxnBatchProperties.getFileConfig().getPageSize(),org);
    }
}
